package com.ruoyi.web.controller.workflow;

import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.common.utils.file.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.io.IOUtils;
import org.checkerframework.checker.units.qual.A;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.workflow.domain.BpmnInfo;
import com.ruoyi.workflow.service.IBpmnInfoService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 流程定义Controller
 * 
 * @author god磊
 * @since 2022-11-14
 */
@RestController
@RequestMapping("/workflow/bpmnInfo")
public class BpmnInfoController extends BaseController
{
    @Autowired
    private IBpmnInfoService bpmnInfoService;

    /**
     * 查询流程定义列表
     */
    @PreAuthorize("@ss.hasPermi('business:bpmnInfo:list')")
    @GetMapping("/list")
    public TableDataInfo list(BpmnInfo bpmnInfo)
    {
        startPage();
        List<BpmnInfo> list = bpmnInfoService.selectBpmnInfoList(bpmnInfo);
        return getDataTable(list);
    }

    /**
     * 删除流程定义
     */
    @PreAuthorize("@ss.hasPermi('business:bpmnInfo:remove')")
    @Log(title = "流程定义", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(bpmnInfoService.deleteDefinition(Arrays.asList(ids)));
    }

    /**
     * 查看流程图/流程文件
     */
    @PreAuthorize("@ss.hasPermi('business:bpmnInfo:readResource')")
    @Log(title = "查看流程资源")
    @GetMapping("/resources/{id}")
    public void getResource(@PathVariable Long id,String type,HttpServletResponse resp) throws IOException {
            // TODO 参数校验: 非空判断
        InputStream resource = bpmnInfoService.getResourceByType(id,type);
        // 将资源拷贝到响应输出流中
        IOUtils.copy(resource,resp.getOutputStream());
    }
    private static final  List<String> SUPPORT_SUFFIXES = Arrays.asList("xml","bpmn","zip");
    /**
     * 部署流程定义
     */
    @PreAuthorize("@ss.hasPermi('business:bpmnInfo:deploy')")
    @Log(title = "部署流程定义", businessType = BusinessType.INSERT)
    @PostMapping("/deploy")
    public AjaxResult deploy(MultipartFile file, BpmnInfo bpmnInfo) throws IOException {
        String filename = file.getOriginalFilename();
       String suffix =  FileUtils.getSuffix(filename);
        if(!SUPPORT_SUFFIXES.contains(suffix)) {
           return AjaxResult.error(HttpStatus.BAD_REQUEST,"目前仅支持提交"+SUPPORT_SUFFIXES+"类型的文件，请重新上传");

       }
        bpmnInfo.setResourceName(filename);
        bpmnInfoService.deploy(file.getInputStream(),bpmnInfo);
        return AjaxResult.success();
    }
}
